package school.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import school.domain.News;
import school.mapper.NewsMapper;
import school.support.BaseController;

@Controller
@RequestMapping("news")
public class NewsController extends BaseController {

    @Autowired
    private NewsMapper newsMapper;

    /**
     * 发表资讯
     *
     * @return
     */
    @GetMapping("send")
    public String send() {
        if (isLogin()) return "send";
        return "redirect:/login.do";
    }

    /**
     * 发表资讯
     *
     * @return
     */
    @PostMapping("send")
    public String send(News news, MultipartFile icon) {
        if (isLogin()) {
            if (icon != null && !icon.isEmpty()) {
                String file = saveFile(icon);
                news.setPath(file);
            }
            news.setUserid(loginUser().getId());
            newsMapper.insert(news);
            return "redirect:/index.do";
        }
        return "redirect:/login.do";

    }
}
